/**
 * Copyright (c) 2017,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.business.login.service;


import com.cargo.base.exceptions.CommonException;
import com.cargo.base.utils.CheckUtil;
import com.cargo.base.utils.PropertiesUtil;

import com.cargo.base.utils.SecurityUtil;
import com.cargo.business.login.mapper.cargo.LoginUserAMapper;
import com.cargo.business.login.vo.SalesUserVO;
import com.cargo.common.controller.InitDictionary;
import com.cargo.security.ShiroUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/** 
 * LoginService:WebSystem用户登录
 * @Project Name:topsales_services 
 * @File Name:LoginService.java 
 * @Package Name:com.cargo.business.topsales.system.login.service
 * @Creator:wizard
 * @Date:2017年9月20日上午9:27:18 
 */
@Service
@Transactional(rollbackFor = java.lang.Throwable.class)
public class LoginService {
	
	@Autowired 
	private LoginUserAMapper loginUserAMapper;

	
	private static final String WEB_MOBILE_CAPTCHA_PREFIX = "WebSystem_register_";
	
	/** 
	 * adminLogin:(用户密码登录).
	 * @param salesUserVO
	 * @return 
	 * @return :ShiroUser 
	 * @Creator:wizard
	 * @Date:2017年9月20日 上午11:29:56
	 */  
	public ShiroUser adminLogin(SalesUserVO salesUserVO) throws Exception {
		// 根据用户名查询用户信息
		String password = salesUserVO.getPassword();
		salesUserVO.setPassword(null);
		//查未删除的用户
		SalesUserVO adminuser = loginUserAMapper.getByUserName(salesUserVO);
		salesUserVO.setPassword(password);
		if (CheckUtil.checkObj(adminuser)) {
			// 检查用户是否已被禁用
			if (CheckUtil.checkObj(adminuser.getStatus()) && adminuser.getStatus().intValue() == InitDictionary.ValueType.SYS_USER_STATUS_JY.value()) {
				throw new CommonException("用户已被禁用");
			}
			// 验证密码
			if (SecurityUtil.toMD5(password).equals(adminuser.getPassword())) {
				ShiroUser shiroUser = new ShiroUser();
				shiroUser.setConnType(2);// 连接方式：2-用户交互
				// 登录系统类型
				String sysSource = PropertiesUtil.getValue("common.syssource.WebSystem");
				String syseType = sysSource.split(":")[1];
				shiroUser.setSysType(Integer.parseInt(syseType));// 登录系统类型(3:WebSystem)
				shiroUser.setUserId(adminuser.getId()); // 登录用户id
				shiroUser.setUserName(adminuser.getUserName()); // 登录用户名
				shiroUser.setNickName(adminuser.getNameCn()); // 登录用户呢称
				shiroUser.setPhoneNumber(adminuser.getMobile()); // 登录用户手机号码
				shiroUser.setCompanyId(adminuser.getCompanyId()); // 登录用户公司ID
				shiroUser.setEmail(adminuser.getEmail()); // 登录用户公司ID
				if(CheckUtil.checkObj(adminuser.getHeadPic())){
					shiroUser.setHeadPic(PropertiesUtil.getValue("common.cmnfilepath")+adminuser.getHeadPic());//登陆用户头像地址
				}else{
					shiroUser.setHeadPic("");
				}
				if(CheckUtil.checkObj(adminuser.getCompanyLogo())){
					shiroUser.setLogo(PropertiesUtil.getValue("common.cmnfilepath")+adminuser.getCompanyLogo());//登陆用户头像地址
				}else{
					shiroUser.setLogo("");
				}
				shiroUser.setCompanyName(adminuser.getCompanyName());//登陆公司名称
				// 检查用户权限
				/*List<String> indexmenu = sysAdminUserMenuService.indexmenu(shiroUser.getUserId());
				if (indexmenu.size() > 0) {
					Set<String> permissionses = new HashSet<String>();
					permissionses.addAll(indexmenu);
					shiroUser.setPermissions(permissionses);
				}*/
				return shiroUser;
			} else {
				throw new CommonException("密码不正确");
			}
		} else {
			throw new CommonException("用户不存在");
		}
	}
	

}
